<layout name="all@index/layout" />
<script type="text/javascript">
    $(function () {
        var school = $('#school');
        school.combobox({
            url: '{$ROOT}/all/option/school?only=0&active=1',
            valueField: 'school',
            textField: 'name'
        });

        var school2 = $('#school2');
        school2.combobox({
            url: '{$ROOT}/all/option/school?only=0&active=1',
            valueField: 'school',
            textField: 'name'
        });

        var team = $('#team');
        team.combobox({
            url: '{$ROOT}/all/option/teamtype?only=1',
            valueField: 'name',
            textField: 'value',
            panelHeight:'auto'
        });
        team.combobox('select', 'C');
        var year=$('#year');
        var term=$('#term');
        $('#dg').datagrid({
            title: '团队成员名单',
            idField: 'id',
            striped: 'true',
            singleSelect: true,
            pagination: 'true',
            rownumbers: true,
            pageSize: 20,
            url: '{$ROOT}/param/team/query',
            queryParams: {team:team.combobox('getValue')},
            toolbar: '#toolbar',
            pageList: [20,50, 100, 200],
            columns: [[
                {field: 'teacherno', title: '教师号', width: 70, align: 'center',sortable:true},
                {field: 'teachername', title: '姓名', width: 100, align: 'center',sortable:true},
                {field:'leader',title:'负责人*',width:70,editor:{type:'checkbox',options:{on:1,off:0}},align:'center',formatter:function(val,rec){
                    if(val==1)
                        return '√';
                    else
                        return '';
                }},
                {field: 'schoolname', title: '学院', width: 150, align: 'center',sortable:true},
                {field: 'teamname', title: '团队名称', width: 150, align: 'center',sortable:true}
            ]],
            //标题行右键菜单
            onHeaderContextMenu: function (e, field) {
                e.preventDefault();
                if (!cmenu_obj.cmenu)//没有的话创建一个
                    $('#dg').datagrid('createColumnMenu', cmenu_obj);
                cmenu_obj.cmenu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            //点击单元格时候的事件
            onClickCell: function (index, field) {
                var tt = $('#dg');
                tt.datagrid('startEditing', {field: field, index: index});
                current_datagrid = tt;
            },
            //数据行上右键菜单
            onRowContextMenu: function (e, rowindex, row) {
                e.preventDefault();
                var tt = $('#dg');
                tt.datagrid('selectRow', rowindex);
                $('#menu').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }
        });

        $('#dg2').datagrid({
            title: '检索教师',
            idField: 'teacherno',
            striped: 'true',
            pagination: 'true',
            rownumbers: true,
            pageSize: 20,
            url: '{$ROOT}/param/team/search',
            queryParams: {teacherno:''},
            toolbar: '#toolbar2',
            pageList: [20,50, 100, 200],
            columns: [[
                {field: 'checkbox', title: '选中', checkbox: true},
                {field: 'teacherno', title: '教师号', width: 100, align: 'center'},
                {field: 'teachername', title: '姓名', width: 150, align: 'center'},
                {field: 'schoolname', title: '学院', width: 150, align: 'center'}
            ]],
            //标题行右键菜单
            onHeaderContextMenu: function (e, field) {
                e.preventDefault();
                if (!cmenu_obj.cmenu)//没有的话创建一个
                    $('#dg2').datagrid('createColumnMenu', cmenu_obj);
                cmenu_obj.cmenu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            //数据行上右键菜单
            onRowContextMenu: function (e, rowindex, row) {
                e.preventDefault();
                var tt = $('#dg2');
                tt.datagrid('selectRow', rowindex);
                $('#menu2').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }
        });
        //筛选督导
        $("#search").click(function () {
            var tt = $('#dg');
            tt.datagrid('loadData', {total: 0, rows: []});
            tt.datagrid('load', {
                year:year.val()
                ,term:term.val()
                ,teacherno: $('#teacherno2').val()
                ,teachername: $('#teachername').val()
                ,team:team.combobox('getValue')
                ,school:school.combobox('getValue')
            });
        });

        //检索教师
        $("#search2").click(function () {
            var tt = $('#dg2');
            tt.datagrid('loadData', {total: 0, rows: []});
            tt.datagrid('clearSelections');
            tt.datagrid('clearChecked');
            tt.datagrid('load', {
                teacherno: $('#teacherno2').val(),
                teachername: $('#teachername2').val(),
                school:$('#school2').combobox('getValue')
            });
        });
        function checkSchool(rows) {
            var count = rows.length;
            for (var i = 0; i < count; i++) { //教师所在学院不是本学院的，且登录账户不在职能部门不允许修改。
                if (rows[i].school != '{$USERINFO.school}' && '{$USERINFO.manage}' != '1') {
                    $.messager.alert('错误', '你无法修改其他学院教师信息（' + rows[i].name + '）！', 'error');
                    $("#cancel").click();
                    return false;
                }
            }
            return true;
        }
        //构建数据集
        function buildDataArray(rows){
            var updateRow=[];
            var length=rows.length;
            for (var i = 0; i < length; i++) {
                var updateObj = {}; //插入的单元格
                updateObj.id=rows[i].id;
                updateObj.team=rows[i].team;
                updateObj.leader=rows[i].leader;
                updateObj.teacherno=rows[i].teacherno;
                updateRow.push(updateObj);
            }
            return updateRow;
        }
        $("#add,#menu_add").click(function () {
            var tt = $('#dg');
            var tt2=$('#dg2');
            var rows=tt2.datagrid('getSelections');
            var length=rows.length;
            for(var i=0;i<length;i++){
                var dgRows=tt.datagrid('getRows');
                var amount=dgRows.length;
                for(var j=0;j<amount;j++){
                    if(rows[i].teacherno==dgRows[j].teacherno){
                        $.messager.alert('错误', rows[i].teacherno+rows[i].teachername+'已在团队成员列表中！' , 'error');
                        return;
                    }
                }
                tt.datagrid('insertRow', {
                    index: 0,
                    row: {
                        id:0
                        ,teacherno:rows[i].teacherno
                        ,teachername:rows[i].teachername
                        ,school:rows[i].school
                        ,schoolname:rows[i].schoolname
                        ,team:team.combobox('getValue')
                        ,teamname:team.combobox('getText')
                        ,leader:0
                    }
                });
            }
            tt2.datagrid('clearSelections');
        });
        $("#remove,#menu_remove").click(function () {
            var tt = $('#dg');
            var row = tt.datagrid('getSelected');
            if (row) {
                tt.datagrid('endEditing');
                var rowIndex = tt.datagrid('getRowIndex', row);
                tt.datagrid('deleteRow', rowIndex);
            }
            else {
                $.messager.alert('错误', '请先选中一条记录', 'error')
            }
        });
        //绑定点击保存按钮事件
        $("#save,#menu_save").click(function () {
            var tt = $('#dg');
            tt.datagrid('endEditing');
            if (tt.datagrid('editIndex') != undefined) return;
            //获取更改的数据行内容
            var rows = tt.datagrid('getRows');
            rows = tt.datagrid('getChanges', 'updated');
            var count = 0;
            var effectRow = {};
            effectRow['year']=year.val();
            effectRow['term']=term.val();
            if (rows.length > 0) {
                if(!checkSchool(rows)) return;
                count += rows.length;
                effectRow["updated"] = $.toJSON(buildDataArray(rows));
            }
            //获取删除的行
            rows = tt.datagrid('getChanges', 'deleted');
            if (rows.length > 0) {
                if(!checkSchool(rows)) return;
                count += rows.length;
                effectRow["deleted"] = $.toJSON(buildDataArray(rows));
            }
            //获取添加的行
            rows = tt.datagrid('getChanges', 'inserted');
            if (rows.length > 0) {
                if(!checkSchool(rows)) return;
                count += rows.length;
                effectRow["inserted"] = $.toJSON(buildDataArray(rows));
            }
            if (count <= 0) { //如果没有任何更新，就退出
                $.messager.alert('提示', '没有数据需要保存！', 'info');
                return;
            }
            $.post('{$ROOT}/param/team/update', effectRow, function (result) {
                if (result.status == 1) {
                    tt.datagrid('acceptChanges');
                    tt.datagrid('reload');
                    $.messager.show({	// show error message
                        title: '成功',
                        msg: result.info
                    });
                } else {
                    $.messager.alert('错误', result.info, 'error');
                }
            }, 'json');
        });
        //取消更改
        $("#cancel,#menu_cancel").click(function () {
            var tt = $('#dg');
            tt.datagrid('rejectChanges');
            current_datagrid = null;
        });
    });
</script>
<div class="container">
    <div id="menu" class="easyui-menu" style="width:150px;">
        <div id='menu_save' data-options="iconCls:'icon icon-save'">保存</div>
        <div data-options="iconCls:'icon icon-remove'" id="menu_remove">删除</div>
        <div id='menu_cancel' data-options="iconCls:'icon icon-cancel'">取消</div>
    </div>
    <div id="menu2" class="easyui-menu" style="width:150px;">
        <div data-options="iconCls:'icon icon-back'" id="menu_add">添加选中的教师</div>
    </div>
    <div id="left" style="width:720px;float:left">
        <div id="toolbar">
            <div>

                <label for="teacherno">教师号：</label><input id="teacherno" class="easyui-validatebox" size="6" value="%"/>
                <label for="teachername">姓名：</label><input id="teachername" class="easyui-validatebox" size="3" value="%"/>
                <label for="school">学院：</label><input id="school" size="13">
                <label for="team">团队：</label><input id="team" size="15">
                <a href="#" class="easyui-linkbutton"  data-options="iconCls:'icon icon-search',plain:'true'" id="search">筛选</a> |
                <a href="#" class="easyui-linkbutton"  data-options="iconCls:'icon icon-remove',plain:'true'" id="remove">删除</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon icon-cancel',plain:'true'" id="cancel">取消</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon icon-save',plain:'true'" id="save">保存</a>
            </div>
        </div>
        <table id="dg"></table>
    </div>
    <div id="right" style="width:530px;float:left;padding-left:5px;">
        <div id="toolbar2">
            <a href="#" class="easyui-linkbutton"  data-options="iconCls:'icon icon-back',plain:'true'" id="add">将选中添加</a>
            <label for="teacherno2">教师号：</label><input id="teacherno2" class="easyui-validatebox" size="7" value="%"/>
            <label for="teachername2">姓名：</label><input id="teachername2" class="easyui-validatebox" size="6" value="%"/>
            <label for="school2">学院：</label>
            <input id="school2" size="13">
            <a href="#" class="easyui-linkbutton"  data-options="iconCls:'icon icon-search',plain:'true'" id="search2">检索</a>
        </div>
        <table id="dg2"></table>
    </div>
    <div class="space"></div>
    <div class="information" style="clear:both">
        <ol>说明：
            <li>在左边表格中设置条件检索教师，然后选中后，点击“将选中添加”按钮，可以同时选中多条记录。学院只能添加本学院的教师为团队成员。</li>
            <li>如果是团队负责人，请在“负责人”列打勾。</li>
            <li>添加、修改、删除团队成员后请点击“保存”提交到数据库。</li>
        </ol>
    </div>
</div>